package com.huice.huiceprovider.dao;

import com.huice.huiceprovider.codegen.tables.S1User;
import com.huice.huiceprovider.codegen.tables.records.S1UserRecord;
import com.huice.huiceprovider.entity.vo.UserVo;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.huice.huiceprovider.codegen.tables.S1User.S1_USER;

/**
 * @Description TODO
 * @Author 赵凯
 * @Date 2021/7/14 17:14
 **/
@Repository
public class UserDao {
    @Autowired
    DSLContext db;

    /**
     * 插入单条记录,插入后获取自增主键
     *
     * @param
     * @return void
     * @author 赵凯
     * @lastEditor
     * @createTime 2021/7/14
     * @editTime 2021/7/14
     */
    public int insert() {
        S1UserRecord s1UserRecord = db.newRecord(S1_USER);
        s1UserRecord.setUsername("罗老师");
        s1UserRecord.setEmail("12345@qq.com");
        s1UserRecord.setAddress("北京");
        s1UserRecord.insert();
        // 这里的id是插入后数据库返回的自增ID，会自动存入record中，可以通过get方法获取
        return s1UserRecord.getId();
    }

    public int delete(int id) {
        S1UserRecord record = db.newRecord(S1_USER);
        record.setId(id);
        int deleteRows = record.delete();
        return deleteRows;
    }

    public int update() {
        S1UserRecord recod = db.newRecord(S1_USER);
        recod.setId(1);
        recod.setUsername("罗永浩");
        recod.setAddress("123456@qq.com");
        recod.update();
        return recod.getId();
    }

    public List<UserVo> select(int id) {
        List<UserVo> s1UserRecords = db.select().from(S1_USER).where(S1_USER.ID.eq(id)).fetchInto(UserVo.class);
        return s1UserRecords;
    }


}
